package org.qiandaosystem.qiandaosystemsource.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.qiandaosystem.qiandaosystemsource.common.ApiResponse;
import org.qiandaosystem.qiandaosystemsource.service.AuthService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {
    
    private final AuthService authService;

    @Data
    public static class RegisterRequest {
        @Schema(description = "用户名", example = "student1")
        private String username;
        
        @Schema(description = "密码", example = "123456")
        private String password;
    }

    @Operation(summary = "用户注册", description = "注册新用户，默认角色为student")
    @PostMapping("/register")
    public ApiResponse<?> register(@RequestBody RegisterRequest request) {
        return authService.register(request.getUsername(), request.getPassword());
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public ApiResponse<?> login(@RequestBody RegisterRequest request) {
        return authService.login(request.getUsername(), request.getPassword());
    }

    @Data
    public static class JoinTeacherRequest {
        @Schema(description = "教师邀请码", example = "ABC123")
        private String teacherKey;
        
        @Schema(description = "学生真实姓名", example = "张三")
        private String realName;
    }

    @Operation(summary = "学生加入教师班级", security = @SecurityRequirement(name = "bearerAuth"))
    @PostMapping("/student/join-teacher")
    public ApiResponse<?> joinTeacher(@RequestBody JoinTeacherRequest request,
                                  @RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        return authService.joinTeacher(jwt, request.getTeacherKey(), request.getRealName());
    }
} 